using Microsoft.EntityFrameworkCore;
using OrderingSystem.Core.Entities;

namespace OrderingSystem.Infrastructure.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Admin> Admins { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Dish> Dishes { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDelivery> OrderDeliveries { get; set; }
        public DbSet<Review> Reviews { get; set; }
        public DbSet<DishDiscussion> DishDiscussions { get; set; }
        public DbSet<News> News { get; set; }
        public DbSet<Favorite> Favorites { get; set; }
        public DbSet<Chat> Chats { get; set; }
        public DbSet<Config> Configs { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置Admin实体映射到users表
            modelBuilder.Entity<Admin>(entity =>
            {
                entity.ToTable("users");
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.AddTime).HasColumnName("addtime");
                entity.Property(e => e.Username).HasColumnName("username");
                entity.Property(e => e.Password).HasColumnName("password");
                entity.Property(e => e.Role).HasColumnName("role");
            });

            // 配置User实体映射到yonghu表
            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("yonghu");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.yonghuming).HasColumnName("yonghuming");
                entity.Property(e => e.mima).HasColumnName("mima");
                entity.Property(e => e.xingming).HasColumnName("xingming");
                entity.Property(e => e.xingbie).HasColumnName("xingbie");
                entity.Property(e => e.touxiang).HasColumnName("touxiang");
                entity.Property(e => e.lianxidianhua).HasColumnName("lianxidianhua");
                
                // 明确忽略IsDeleted和Role属性，这些属性在数据库表中不存在
                // 使用Ignore方法确保这些属性不会包含在SQL查询中
                entity.Ignore(e => e.IsDeleted);
                entity.Ignore(e => e.Role);
            });

            // 配置Category实体映射到caipinfenlei表
            modelBuilder.Entity<Category>(entity =>
            {
                entity.ToTable("caipinfenlei");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.caipinfenlei).HasColumnName("caipinfenlei");
            });

            // 配置Dish实体映射到caipinxinxi表
            modelBuilder.Entity<Dish>(entity =>
            {
                entity.ToTable("caipinxinxi");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.caipinmingcheng).HasColumnName("caipinmingcheng");
                entity.Property(e => e.caipinfenlei).HasColumnName("caipinfenlei");
                entity.Property(e => e.tupian).HasColumnName("tupian");
                entity.Property(e => e.caipinjiage).HasColumnName("caipinjiage");
                entity.Property(e => e.cailiao).HasColumnName("cailiao");
                entity.Property(e => e.fenliang).HasColumnName("fenliang");
                entity.Property(e => e.caipinjieshao).HasColumnName("caipinjieshao");
                entity.Property(e => e.clicktime).HasColumnName("clicktime");
                entity.Property(e => e.clicknum).HasColumnName("clicknum");
                
                // 忽略ShangJia属性，因为数据库中没有对应的列
                entity.Ignore(e => e.shangjia);
            });

            // 配置Order实体映射到dingdanxinxi表
            modelBuilder.Entity<Order>(entity =>
            {
                entity.ToTable("dingdanxinxi");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.dingdanbianhao).HasColumnName("dingdanbianhao");
                entity.Property(e => e.caipinmingcheng).HasColumnName("caipinmingcheng");
                entity.Property(e => e.caipinfenlei).HasColumnName("caipinfenlei");
                entity.Property(e => e.tupian).HasColumnName("tupian");
                entity.Property(e => e.caipinjiage).HasColumnName("caipinjiage");
                entity.Property(e => e.shuliang).HasColumnName("shuliang");
                entity.Property(e => e.zongjine).HasColumnName("zongjine");
                entity.Property(e => e.xiadanshijian).HasColumnName("xiadanshijian");
                entity.Property(e => e.songcandizhi).HasColumnName("songcandizhi");
                entity.Property(e => e.yonghuming).HasColumnName("yonghuming");
                entity.Property(e => e.lianxidianhua).HasColumnName("lianxidianhua");
                entity.Property(e => e.sfsh).HasColumnName("sfsh");
                entity.Property(e => e.shhf).HasColumnName("shhf");
                entity.Property(e => e.ispay).HasColumnName("ispay");
                entity.Property(e => e.userid).HasColumnName("userid");

                // 配置与User的关系
                entity.HasOne(d => d.User)
                      .WithMany()
                      .HasForeignKey(d => d.userid);
            });

            // 配置Review实体映射到caipinpingjia表
            modelBuilder.Entity<Review>(entity =>
            {
                entity.ToTable("caipinpingjia");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.caipinmingcheng).HasColumnName("caipinmingcheng");
                entity.Property(e => e.caipinfenlei).HasColumnName("caipinfenlei");
                entity.Property(e => e.pingfen).HasColumnName("pingfen");
                entity.Property(e => e.pingjianeirong).HasColumnName("pingjianeirong");
                entity.Property(e => e.huifuneirong).HasColumnName("shhf");
                entity.Property(e => e.yonghuming).HasColumnName("yonghuming");
                entity.Property(e => e.userid).HasColumnName("userid");

                // 配置与User的关系
                entity.HasOne(d => d.User)
                      .WithMany()
                      .HasForeignKey(d => d.userid);

                // 完全忽略DishId属性和Dish导航属性
                entity.Ignore(e => e.dishid);
                entity.Ignore(e => e.Dish);
                
                // 忽略兼容性属性
                entity.Ignore(e => e.Id);
                entity.Ignore(e => e.AddTime);
                entity.Ignore(e => e.CaipinMingCheng);
                entity.Ignore(e => e.CaipinFenlei);
                entity.Ignore(e => e.PingFen);
                entity.Ignore(e => e.PingJiaNeiRong);
                entity.Ignore(e => e.HuiFuNeiRong);
                entity.Ignore(e => e.YongHuMing);
                entity.Ignore(e => e.UserId);
                entity.Ignore(e => e.DishId);
            });

            // 配置DishDiscussion实体映射到discusscaipinxinxi表
            modelBuilder.Entity<DishDiscussion>(entity =>
            {
                entity.ToTable("discusscaipinxinxi");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.content).HasColumnName("content");
                entity.Property(e => e.reply).HasColumnName("reply");
                entity.Property(e => e.userid).HasColumnName("userid");
                entity.Property(e => e.refid).HasColumnName("refid");

                // 忽略数据库中不存在的属性
                entity.Ignore(e => e.caipinfenlei);
                entity.Ignore(e => e.caipinmingcheng);
                entity.Ignore(e => e.yonghuming);

                // 忽略兼容性属性
                entity.Ignore(e => e.Id);
                entity.Ignore(e => e.AddTime);
                entity.Ignore(e => e.CaipinFenlei);
                entity.Ignore(e => e.CaipinMingCheng);
                entity.Ignore(e => e.TaoLunNeiRong);
                entity.Ignore(e => e.HuiFuNeiRong);
                entity.Ignore(e => e.YongHuMing);
                entity.Ignore(e => e.UserId);
                entity.Ignore(e => e.DishId);

                // 配置与User的关系
                entity.HasOne(d => d.User)
                      .WithMany()
                      .HasForeignKey(d => d.userid);

                // 配置与Dish的关系
                entity.HasOne(d => d.Dish)
                      .WithMany()
                      .HasForeignKey(d => d.refid);
            });

            // 配置News实体映射到news表
            modelBuilder.Entity<News>(entity =>
            {
                entity.ToTable("news");
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.AddTime).HasColumnName("addtime");
                entity.Property(e => e.BiaoTi).HasColumnName("title");
                entity.Property(e => e.JianJie).HasColumnName("introduction");
                entity.Property(e => e.TuPian).HasColumnName("picture");
                entity.Property(e => e.NeiRong).HasColumnName("content");
                
                // 忽略数据库中不存在的属性
                entity.Ignore(e => e.ClickNum);
            });

            // 配置Favorite实体映射到storeup表
            modelBuilder.Entity<Favorite>(entity =>
            {
                entity.ToTable("storeup");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.mingcheng).HasColumnName("name");
                entity.Property(e => e.tupian).HasColumnName("picture");
                entity.Property(e => e.leixing).HasColumnName("tablename");
                entity.Property(e => e.userid).HasColumnName("userid");
                entity.Property(e => e.refid).HasColumnName("refid");
                
                // 忽略数据库中不存在的属性
                entity.Ignore(e => e.jiage);
                entity.Ignore(e => e.yonghuming);

                // 忽略兼容性属性
                entity.Ignore(e => e.Id);
                entity.Ignore(e => e.AddTime);
                entity.Ignore(e => e.MingCheng);
                entity.Ignore(e => e.TuPian);
                entity.Ignore(e => e.LeiXing);
                entity.Ignore(e => e.JiaGe);
                entity.Ignore(e => e.YongHuMing);
                entity.Ignore(e => e.UserId);
                entity.Ignore(e => e.RefId);

                // 配置与User的关系
                entity.HasOne(d => d.User)
                      .WithMany()
                      .HasForeignKey(d => d.userid);
            });

            // 配置Chat实体映射到chat表
            modelBuilder.Entity<Chat>(entity =>
            {
                entity.ToTable("chat");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.addtime).HasColumnName("addtime");
                entity.Property(e => e.LiaoTianNeiRong).HasColumnName("ask");
                entity.Property(e => e.reply).HasColumnName("reply");
                entity.Property(e => e.isreply).HasColumnName("isreply");
                entity.Property(e => e.userid).HasColumnName("userid");

                // 配置与User的关系
                entity.HasOne(d => d.User)
                      .WithMany()
                      .HasForeignKey(d => d.userid);
            });

            // 配置Config实体映射到config表
            modelBuilder.Entity<Config>(entity =>
            {
                entity.ToTable("config");
                entity.Property(e => e.id).HasColumnName("id");
                entity.Property(e => e.name).HasColumnName("name");
                entity.Property(e => e.value).HasColumnName("value");
            });
        }
    }
} 